<%--
  Created by IntelliJ IDEA.
  User: niyaz
  Date: 9/8/12
  Time: 12:10 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
</head>
<body>

<%@ page import="java.*" %>
<%@ page import="org.apache.solr.client.solrj.SolrQuery" %>
<%@ page import="org.apache.solr.client.solrj.SolrServer" %>
<%@ page import="org.apache.solr.client.solrj.impl.HttpSolrServer" %>
<%@ page import="gui.FieldProvider" %>
<%@ page import="gui.DefaultFieldProvider" %>
<%@ page import="gui.Field" %>
<%@ page import="org.apache.solr.client.solrj.SolrResponse" %>
<%@ page import="org.apache.solr.client.solrj.response.QueryResponse" %>
<%@ page import="gui.EnumableField" %>
<%@ page import="java.util.Arrays" %>
<%@ page import="org.apache.solr.client.solrj.response.FacetField" %>
<%@ page import="java.util.List" %>
<%@ page import="org.apache.solr.common.SolrDocument" %>

<%!
    SolrServer solrServer = new HttpSolrServer("http://127.0.0.1:8983/apollo");
    FieldProvider fieldProvider = new DefaultFieldProvider();
    String PHRASE = "phrase";
    String PHOTO = "photo_big";
    String UID = "uid";
%>

<%
    SolrQuery query = new SolrQuery();
    if (request.getParameterMap().keySet().isEmpty()) {
        query.setQuery("*:*");
    } else {
        String phrase = request.getParameter(PHRASE);
        if (phrase == null || phrase.equals("")) {
            phrase = "*:*";
        } else {
            phrase = "{!dismax tie=0.1}" + phrase;
        }
        query.setQuery(phrase);
        query.setFacet(true);
        query.setHighlight(true);
        query.addHighlightField("*");

        for (Object parameterKey : request.getParameterMap().keySet()) {

            String field = (String) parameterKey;
            if (parameterKey.equals(/*"{!dismax tie=0.1}"+*/PHRASE) || field.contains("_2")) {
                continue;
            }
            String[] values = request.getParameterValues(field);

            switch (fieldProvider.getTypeByField(field)) {
                case CHECK_BOX:
                case RADIO_BOX:
                case LIST: {
                    String filter = "";
                    for (String value : values) {
                        if (!filter.equals("")) {
                            filter += " OR ";
                        }
                        filter += field + ":" + value;
                    }
                    query.addFilterQuery(filter);
                    break;
                }
                case TEXT: {
                    if (values[0] != null && !values[0].equals(""))
                        query.addFilterQuery(field + ":" + values[0]);
                    break;
                }
                case RANGE: {
                    String upper = "*";
                    String down = "*";
                    if (values[0] != null && !values[0].equals("")) {
                        down = values[0];
                    }
                    String[] second = request.getParameterValues(field + "_2");
                    if (second[0] != null && !second[0].equals("")) {
                        upper = second[0];
                    }
                    if (!upper.equals("*") || !down.equals("*")) {
                        query.addFilterQuery(field + ":[" + down + " TO " + upper + "]");
                    }
                }
            }
        }


    }

    for (Field field : fieldProvider.getEnumableFields()) {
        query.addFacetField(field.getFieldName());
    }
    for (String field : fieldProvider.getViewableFields()) {
        query.addField(field);
    }
    query.setHighlightSnippets(10);
    query.addField(PHOTO);
    query.addField(UID);


    QueryResponse solrResponse = solrServer.query(query);
    out.println(solrResponse.getResults().getNumFound());
%>

<table border="1" align="center" width="100%">
    <%--    <tr width="100%">
        <td>
            <form action="index.jsp">
                <input type="text" name="raw"/>
            </form>
        </td>
    </tr>--%>
    <tr>
        <td width="70%">
            <table border="1" width="100%">
                <%
                    for (SolrDocument person : solrResponse.getResults()) {
                %>
                <tr width="100%">
                    <td>
                        <a href="http://vk.com/id<%= person.getFieldValue(UID) %>">
                            <img src="<%= person.getFieldValue(PHOTO)%>" alt="<%= person.getFieldValue(PHOTO)%>"
                                 align="left"> </a> <%
                        for (String field : fieldProvider.getViewableFields()) {

                            out.print(fieldProvider.getFieldByName(field).getFieldTextRepresentive() + " = ");
                            out.print(person.getFieldValue(field) + "</br>");

                        }

                    %>Совпадение: <br/><%
                        for (List<String> list : solrResponse.getHighlighting().get(person.getFieldValue(UID)).values()) {
                            for (String high : list) {
                                out.println(high + "<br/>");
                            }
                        }


                    %></td>
                </tr>
                <%
                    }

                %>

                </td>
                </tr>


            </table>
        </td>
        <td valign="top">
            <form action="index.jsp">
                <table border="2" align="left">
                    <tr>
                        <td>
                            <b>Основный запрос по всем полям</b> <br/>
                            <input type="text" name="phrase"
                                    <%=
                                    request.getParameterValues(PHRASE) != null ? "value=\"" + request.getParameterValues(PHRASE)[0] + "\"" : ""
                                    %>/>
                        </td>
                    </tr>

                    <%-- Check boxes --%>

                    <%
                        for (EnumableField field : fieldProvider.getEnumableFields()) {
                    %>
                    <tr>
                        <td><%
                            for (String value : field.getValues()) {
                                List<FacetField.Count> counts = solrResponse.getFacetField(field.getFieldName()).getValues();
                                long num = 0;
                                for (FacetField.Count count : counts) {
                                    if (count.getName().equals(value)) {
                                        num = count.getCount();
                                    }
                                }
                        %>
                            <input type="checkbox"
                                   name="<%= field.getFieldName() %>"
                                   value="<%= value %>"

                                <%= ((request.getParameterValues(field.getFieldName()) != null) &&
                                   (Arrays.asList(request.getParameterValues(field.getFieldName())).contains(value)))
                                    ? "checked=\"true\""
                                    : "" %>
                                    >
                            <%= field.getNameByValue(value) + " (" + num + ")"%>

                            </input>
                            <br/>
                            <%
                                }
                            %>
                        </td>
                    </tr>
                    <%}%>

                    <%-- Text fields--%>
                    <%
                        for (Field field : fieldProvider.getTextFields()) {
                    %>
                    <tr>
                        <td>
                            <b><%= field.getFieldTextRepresentive()%>
                            </b>
                            <input type="text" name="<%=field.getFieldName()%>"
                                    <%=
                                    request.getParameterValues(field.getFieldName()) != null ? " value=\"" + request.getParameterValues(field.getFieldName())[0] + "\"" : ""
                                    %>/>

                        </td>
                    </tr>
                    <%}%>

                    <%-- Range fields--%>
                    <%
                        for (Field field : fieldProvider.getRangeFields()) {
                    %>
                    <tr>
                        <td>
                            <b><%= field.getFieldTextRepresentive()%>
                            </b><br/>
                            от
                            <input type="text" name="<%=field.getFieldName()%>"
                                    <%=
                                    request.getParameterValues(field.getFieldName()) != null ? " value=\"" + request.getParameterValues(field.getFieldName())[0] + "\"" : ""
                                    %>/>
                            до
                            <input type="text" name="<%=field.getFieldName()%>_2"
                                    <%=
                                    request.getParameterValues(field.getFieldName() + "_2") != null ? " value=\"" + request.getParameterValues(field.getFieldName() + "_2")[0] + "\"" : ""
                                    %>/>
                        </td>
                    </tr>
                    <%}%>

                </table>
                <input type="submit"/>
            </form>
        </td>
    </tr>
</table>
</body>
</html>